package arch.dao.impl.manager;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import arch.dao.manager.ConfigDao;
import arch.domain.common.Config;
/**
 * 
 * @author ZhaoWangLong Sep 17, 2012
 *
 */
@Repository("configDao")
public class ConfigImpl implements ConfigDao {
	@Resource private JdbcTemplate jdbcTemplate;
	private final Logger logger = Logger.getLogger(ConfigImpl.class);
	
	public Config find(Serializable primaryKey) {
		String sql = "select CONFIG_ID, MODULE_KEY, MODULE_VALUE, ITEM_KEY, ITEM_VALUE "
			+"  from AD_CONFIG where CONFIG_ID = ? ";
		logger.debug("SQL > "+sql);
		
		try {
			return (Config)this.jdbcTemplate.queryForObject(sql, new Object[]{primaryKey},new RowMapper(){
				public Object mapRow(ResultSet rs, int i) throws SQLException {
					Config config = new Config();
					config.setConfigId(rs.getString("CONFIG_ID"));
					config.setModuleKey(rs.getString("MODULE_KEY"));
					config.setModuleValue(rs.getString("MODULE_VALUE"));
					config.setItemKey(rs.getString("ITEM_KEY"));
					config.setItemValue(rs.getString("ITEM_VALUE"));
					
					return config;
				}});
		} catch (EmptyResultDataAccessException e) {
			return null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void merge(Config entity) {
		String sql = "update AD_CONFIG set MODULE_KEY = ?, MODULE_VALUE = ?, ITEM_KEY = ?, ITEM_VALUE = ? "
			+" where CONFIG_ID = ? ";
		
		this.jdbcTemplate.update(sql,new Object[]{
				entity.getModuleKey(),
				entity.getModuleValue(),
				entity.getItemKey(),
				entity.getItemValue(),
				entity.getConfigId()
		});
	}

	public void persist(Config entity) {
		String sql = "insert into AD_CONFIG(CONFIG_ID, MODULE_KEY, MODULE_VALUE, ITEM_KEY, ITEM_VALUE) values "
			+" (?,?,?,?,?) ";
		
		this.jdbcTemplate.update(sql,new Object[]{
				entity.getConfigId(),	
				entity.getModuleKey(),
				entity.getModuleValue(),
				entity.getItemKey(),
				entity.getItemValue()
		});
	}

	public void remove(Serializable primaryKey) {
		String sql = "delete from AD_CONFIG where CONFIG_ID = ? ";
		
		this.jdbcTemplate.update(sql,new Object[]{primaryKey});
	}

	@SuppressWarnings("unchecked")
	public List<Config> queryBySQL(String sql, Object... objects) {
		return this.jdbcTemplate.query(sql,objects, new RowMapper(){
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				Config config = new Config();
				config.setConfigId(rs.getString("CONFIG_ID"));
				config.setModuleKey(rs.getString("MODULE_KEY"));
				config.setModuleValue(rs.getString("MODULE_VALUE"));
				config.setItemKey(rs.getString("ITEM_KEY"));
				config.setItemValue(rs.getString("ITEM_VALUE"));
				
				return config;
			}});
	}

}
